package com.fadadianqi.logistics.admin.controller;

import com.fadadianqi.commons.BackToURLUtils;
import com.fadadianqi.commons.ParamHelper;
import com.fadadianqi.goods.model.Brand;
import com.fadadianqi.goods.model.Category;
import com.fadadianqi.goods.model.Goods;
import com.fadadianqi.goods.service.GoodsService;
import com.fadadianqi.logistics.inventory.model.Batch;
import com.fadadianqi.logistics.inventory.model.Storehouse;
import com.fadadianqi.logistics.inventory.service.BatchService;
import com.fadadianqi.logistics.inventory.service.InventoryService;
import com.fadadianqi.logistics.inventory.service.StorehouseService;
import com.yeqiangwei.commons.util.BeanUtils;
import com.yeqiangwei.commons.util.Pagination;
import com.yeqiangwei.commons.util.PaginationHelper;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class BatchController {

    private static final int MAX_RESULTS = 20;
    private static final String VIEW_LSIT = "/admin/batch/list";

    private static final String PARAM_BRAND_ID = "bid";
    private static final String PARAM_CATEGORY_ID = "cid";
    private static final String PARAM_BACK_URL = "backURL";

    @RequestMapping(value = "/a/ajax/batch/list")
    public String showAjaxList(HttpServletRequest request, ModelMap modelMap) {
        BackToURLUtils.setCurrentURLToAttr(request, PARAM_BACK_URL);
        Pagination pagination = new Pagination(request, 100);
        String goodsId = ParamHelper.getGoodsId(request, null);
        List<Batch> list = batchService.findAll(goodsId, null, null, pagination);
        List<BatchVO> voList = new ArrayList<BatchVO>();
        if (ValidatorHelper.isNotEmpty(list)) {
            for (Batch batch : list) {
                BatchVO vo = new BatchVO();
                BeanUtils.copyProperties(vo, batch);
                //
                int a = inventoryService.countNormalStatusByBatchId(batch.getId());
                vo.setStatusNormalCount(a);
                vo.setBrand(goodsService.getBrandById(vo.getBrandId()));
                vo.setGoods(goodsService.getGoodsById(vo.getGoodsId()));
                vo.setCategory(goodsService.getCategoryById(vo.getGoods().getCategoryId()));
                vo.setStorehouse(storehouseService.getStorehouseById(vo.getStorehouseId()));
                voList.add(vo);
            }
        }
        /*pagination.setTotal(batchService.count(goodsId, null, null));
		Map<String, Object> elements = new HashMap<String, Object>();
		pagination.setElements(elements);
		PaginationHelper.setAttribute(request, pagination);*/
        modelMap.put("command", voList);
        return "/admin/batch/ajaxList";
    }

    @RequestMapping(value = "/a/batch/list")
    public String showList(HttpServletRequest request, ModelMap modelMap) {
        BackToURLUtils.setCurrentURLToAttr(request, PARAM_BACK_URL);
        Pagination pagination = new Pagination(request, MAX_RESULTS);
        String categoryId = ParamHelper.getCategoryId(request, null);
        String brandId = ParamHelper.getBrandId(request, null);
        String goodsId = ParamHelper.getGoodsId(request, null);
        List<Batch> list = batchService.findAll(goodsId, brandId, categoryId, pagination);
        List<BatchVO> voList = new ArrayList<BatchVO>();
        if (ValidatorHelper.isNotEmpty(list)) {
            for (Batch batch : list) {
                BatchVO vo = new BatchVO();
                BeanUtils.copyProperties(vo, batch);
                //
                int a = inventoryService.countNormalStatusByBatchId(batch.getId());
                vo.setStatusNormalCount(a);
                vo.setBrand(goodsService.getBrandById(vo.getBrandId()));
                vo.setGoods(goodsService.getGoodsById(vo.getGoodsId()));
                vo.setCategory(goodsService.getCategoryById(vo.getGoods().getCategoryId()));
                vo.setStorehouse(storehouseService.getStorehouseById(vo.getStorehouseId()));
                voList.add(vo);
            }
        }
        pagination.setTotal(batchService.count(goodsId, brandId, categoryId));
        Map<String, Object> elements = new HashMap<String, Object>();
        elements.put(PARAM_BRAND_ID, brandId);
        elements.put(PARAM_CATEGORY_ID, categoryId);
        pagination.setElements(elements);
        PaginationHelper.setAttribute(request, pagination);

        modelMap.put("voList", voList);
        modelMap.put("brandList", getBrandList(null));
        modelMap.put("categoryList", getCategoryList(null));
        modelMap.put(PARAM_BRAND_ID, brandId);
        modelMap.put(PARAM_CATEGORY_ID, categoryId);

        return VIEW_LSIT;
    }

    private List<Category> getCategoryList(String keys) {
        List<Category> categoryList = goodsService.findAllCategory();
        return categoryList;
    }

    private List<Brand> getBrandList(String keys) {
        List<Brand> brandList = goodsService.findAllBrand();
        return brandList;
    }

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private BatchService batchService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private StorehouseService storehouseService;

    public static class BatchVO extends Batch {

        /**
         *
         */
        private static final long serialVersionUID = 8738427502673486834L;

        private Goods goods;
        private Category category;
        private Brand brand;
        private int statusNormalCount = 0; // 库存数量
        private Storehouse storehouse;

        public Storehouse getStorehouse() {
            return storehouse;
        }

        public void setStorehouse(Storehouse storehouse) {
            this.storehouse = storehouse;
        }

        public int getStatusNormalCount() {
            return statusNormalCount;
        }

        public void setStatusNormalCount(int statusNormalCount) {
            this.statusNormalCount = statusNormalCount;
        }

        public Goods getGoods() {
            return goods;
        }

        public void setGoods(Goods goods) {
            this.goods = goods;
        }

        public Category getCategory() {
            return category;
        }

        public void setCategory(Category category) {
            this.category = category;
        }

        public Brand getBrand() {
            return brand;
        }

        public void setBrand(Brand brand) {
            this.brand = brand;
        }
    }
}
